const getProductFamily = async dataObj => {
  const { data: res } = await axios({
    method: 'POST',
    url: '../handler/report/CommonActionHandle.ashx?action=getProductFamily',
    data: {
      type: 'getProductFamily'
    }
  })
  dataObj.productFamilyList = res.data;
  return res.data;
}

const getFactory = async dataObj => {
  const { data: res } = await axios({
    method: 'POST',
    url: '../handler/report/CommonActionHandle.ashx?action=getFactory',
    data: {
      type: 'getFactory'
    }
  })
  dataObj.factoryList = res.data
  return res.data
}

const exportExcel = async (config, dataObj, ElMessage) => {
  dataObj.loading = true
  dataObj.loadingText = '导出中...'
  const res = await axios({
    method: 'post',
    url: config.url,
    responseType: 'arraybuffer',
    data: { type: 'exportExcel', ...dataObj.queryInfo }
  }).then((res) => {
    dataObj.loading = false;
    ElMessage.success('导出成功')
    let blob = new Blob([res.data]);
    let link = document.createElement('a');
    let href = window.URL.createObjectURL(blob);
    link.style.display = 'none';
    link.href = href;
    link.download = decodeURI(res.headers["content-disposition"].split('filename=')[1]);
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    window.URL.revokeObjectURL(href);
  }).catch((error) => {
    dataObj.loading = false
    if (error.response.status === 500) {
      ElMessage.error('导出时出错');
    } else if (error.response.status === 404) {
      ElMessage.error('没有数据，不能导出');
    }
  })
}

const fetchSuggestion = async (config, ElMessage) => {
  const triggerLength = config.triggerLength || 4;
  if (config.value && config.value.length >= triggerLength) {
    const { data: res } = await axios.post(`../handler/report/CommonActionHandle.ashx?action=${config.actionName}`, {
      ...config.restConfig,
      type: config.actionName,
      [config.name]: config.value
    })
    if (res.meta.status === 500) {
      return ElMessage.error(res.meta.msg);
    }
    const result = res.data.filter(item => item.rownum < 100);
    config.callback(result);
  }
}

const getParams = name => {
  try {
    var url = window.document.location.href;
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
    var r = url.split('?')[1].match(reg);
    if (r != null) {
      return r[2];
    }
    return "";
  } catch (e) {
    return "";
  }
}

const getUserWhiteList = async (pageurl, ElMessage) => {
  const { data: res } = await axios.post('../handler/report/CommonActionHandle.ashx?action=getUserWhiteList', {
    type: 'getUserWhiteList',
    pageurl
  })
  let whiteList = [];
  if (res.meta.status === 500) {
    return ElMessage.error(res.meta.msg);
  }
  res.data.forEach(item => whiteList.push(item.employeeid));
  return whiteList;
}

const exportExcelData = async (config, _this) => {
  _this.loading = true
  _this.loadingText = '导出中...'
  const { url, data } = config
  await axios({
    method: 'post', responseType: 'arraybuffer', url, data
  }).then(res => {
    _this.loading = false;
    if (!res.headers["content-disposition"]) {
      return _this.$message.error(`导出时出错!`);
    }
    _this.$message.success('导出成功!')
    let blob = new Blob([res.data]);
    let link = document.createElement('a');
    let href = window.URL.createObjectURL(blob);
    link.style.display = 'none';
    link.href = href;
    link.download = decodeURI(res.headers["content-disposition"].split('filename=')[1]);
    document.body.appendChild(link);
    link.click();
    document.body.removeChild(link);
    window.URL.revokeObjectURL(href);
  }).catch(error => {
    _this.loading = false
    _this.$message.error('没有数据，不能导出!');
  })
}

// 生成uuid
const generateUUID = () => {
  let d = new Date().getTime(); // 时间戳
  let uuid = 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function (c) {
    let r = (d + Math.random() * 16) % 16 | 0;
    d = Math.floor(d / 16);
    return (c == 'x' ? r : (r & 0x3 | 0x8)).toString(16);
  });
  return uuid;
}

export { getProductFamily, exportExcel, getFactory, fetchSuggestion, getParams, getUserWhiteList, exportExcelData, generateUUID }